package com.core.enums;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: stefanie-zy
 * @time: 2024/1/24-16:47
 * @description: Enum-公共继承
 */
@Schema(name = "Enum-公共继承")
public interface CoreEnum {

    String getMessage();

    Integer getCode();

    @Operation(summary = "根据枚举、枚举状态码获取枚举")
    static <E extends CoreEnum> E getEnumByCode(Class<E> enumClass, Integer code) {
        E e = null;
        if (enumClass != null && code != null && enumClass.isEnum()) {
            E[] enums = enumClass.getEnumConstants();
            for (E ec : enums) {
                if (ec.getCode().equals(code)) {
                    e = ec;
                    break;
                }
            }
        }
        return e;
    }

    @Operation(summary = "据枚举、枚举状态码获取枚举值")
    static <E extends CoreEnum> String getMessageByCode(Class<E> enumClass, Integer code) {
        String message = null;
        if (enumClass != null && code != null && enumClass.isEnum()) {
            E[] enums = enumClass.getEnumConstants();
            for (E ec : enums) {
                if (ec.getCode().equals(code)) {
                    message = ec.getMessage();
                    break;
                }
            }
        }
        return message;
    }

    @Operation(summary = "获取枚举键值对")
    static <E extends CoreEnum> List<Map<Integer, String>> getEnumList(Class<E> enumClass) {
        List<Map<Integer, String>> enumList = new ArrayList<>();
        if (enumClass != null && enumClass.isEnum()) {
            E[] enums = enumClass.getEnumConstants();
            for (E ec : enums) {
                Map<Integer, String> map = new HashMap<>();
                map.put(ec.getCode(), ec.getMessage());
                enumList.add(map);
            }
        }
        return enumList;
    }

    @Operation(summary = "获取枚举状态码列表")
    static <E extends CoreEnum> List<Integer> getCodeList(Class<E> enumClass) {
        List<Integer> codeList = new ArrayList<>();
        if (enumClass != null && enumClass.isEnum()) {
            E[] enums = enumClass.getEnumConstants();
            for (E ec : enums) {
                codeList.add(ec.getCode());
            }
        }
        return codeList;
    }

    @Operation(summary = "判断枚举是否包含值")
    static <E extends CoreEnum> Boolean check(Class<E> enumClass, Integer code) {
        List<Integer> codeList = getCodeList(enumClass);
        return codeList.contains(code);
    }
}
